// 将系统的表单清空
function testSystemCleanForm(type) {
    if (type == 0 || type == 2) {//如果是不可编辑的表单的话
        testSystemTabPanel.setActiveTab(0);
        testSystemDetailForm.reset();
    } else {//可编辑的
        testSystemEditorTabPanel.setActiveTab(0);
        testSystemDetailEditorForm.reset();
        testSystemDetailEditorForm.down('#name').setReadOnly(false);
        testSystemDetailEditorForm.down('#type').setReadOnly(false);
        testSystemDetailEditorForm.down('#describe').setReadOnly(false);
        testSystemEditorTabPanel.down('#saveButton').setDisabled(false);
        // testSystemEditorGrid.down('#addSystemButton').setDisabled(false);
        // testSystemEditorGrid.down('#deleteSystemButton').setDisabled(false);
    }
    testSystemAttachedStore.removeAll();
}

// 获取系统的详细信息
function testSystemGetDetail(id, type) {
    var result;
    var url = zuulUrl + '/' + testServiceName;
    if (type == 0)//判断id是系统Id还是项目Id
        url = url + '/testProject/system/detail/';
    else
        url = url + '/testSystem/detail/';
    Ext.Ajax.request({// 获取当前节点的信息
        url: url + id,
        async: false, // ASYNC 是否异步( TRUE 异步 , FALSE 同步)
        success: function (response, opts) {
            var obj = Ext.decode(response.responseText);
            obj.testSystem.createDate = obj.testSystem.createDate.replace('T',
                '  ');
            obj.testSystem.updateDate = obj.testSystem.updateDate.replace('T',
                '  ');
            result = obj;
        },
        failure: function (response, opts) {
            var obj = Ext.decode(response.responseText);
            if (obj.code == 406 || obj.code == 407)
                Ext.Msg.alert("错误", obj.message);
            else
                Ext.Msg.alert("错误", "该功能暂时不可用！");
        }
    });
    return result;
}

// 将系统的详细信息加载到表单中
function testSystemPutDataIntoForm(data, type) {
    var basic = data.testSystem, attachedList = data.attachedList;
    if (type == 0 || type == 2) {
        testSystemDetailForm.down('#name').setValue(basic.name);
        testSystemDetailForm.down('#type').setValue(basic.type);
        testSystemDetailForm.down('#addMan').setValue(basic.addManName);
        testSystemDetailForm.down('#devMan').setValue(basic.devMan);
        testSystemDetailForm.down('#createDate').setValue(basic.createDate);
        testSystemDetailForm.down('#updateDate').setValue(basic.updateDate);
        testSystemDetailForm.down('#describe').setValue(basic.describe);
    } else {
        if (basic.addMan != currentUserId) {//不是自己的不能修改
            testSystemDetailEditorForm.down('#name').setReadOnly(true);
            testSystemDetailEditorForm.down('#type').setReadOnly(true);
            testSystemDetailEditorForm.down('#describe').setReadOnly(true);
            testSystemEditorTabPanel.down('#saveButton').setDisabled(true);
            // testSystemEditorGrid.down('#addSystemButton').setDisabled(true);
            // testSystemEditorGrid.down('#deleteSystemButton').setDisabled(true);
        }
        testSystemDetailEditorForm.down('#name').setValue(basic.name);
        testSystemDetailEditorForm.down('#type').setValue(basic.type);
        testSystemDetailEditorForm.down('#addMan').setValue(basic.addManName);
        testSystemDetailEditorForm.down('#devMan').setValue(basic.devMan);
        testSystemDetailEditorForm.down('#createDate').setValue(basic.createDate);
        testSystemDetailEditorForm.down('#updateDate').setValue(basic.updateDate);
        testSystemDetailEditorForm.down('#describe').setValue(basic.describe);
    }
    testSystemAttachedStore.setData(attachedList);
}

// 点击查看详情之后的操作
function testSystemSeeDetailClick(record, type) {
    Ext.getCmp('centerRegion').removeAll(false);
    testSystemCleanForm(type);
    var data = testSystemGetDetail(record.data.id);
    testSystemPutDataIntoForm(data, type);
    if (type == 0 || type == 2)
        Ext.getCmp('centerRegion').add(testSystemTabPanel);
    else {
        Ext.getCmp('centerRegion').add(testSystemEditorTabPanel);
    }
}

//当文件上传框变化之后触发的函数
function getFilePath() {
    var fileEl = document.getElementById('systemAttaInput');
    testSystemFileArray.push(fileEl.files[0]);
    var filePath = fileEl.value;    //js中无法获取文件的真是路径
    var size = Math.floor(fileEl.files[0].size / 1024);//文件大小
    var fileName = filePath.substring(filePath.lastIndexOf('\\') + 1); //文件名
    var extName = fileName.substring(fileName.lastIndexOf('.') + 1);    //后缀名
    var type;//文件类型
    if (extName.toLowerCase() == 'jpg'
        || extName.toLowerCase() == 'png'
        || extName.toLowerCase() == 'gif'
        || extName.toLowerCase() == 'jpeg') {
        type = 0;
    } else
        type = 1;
    var newRecord = {
        id: Ext.create("MyApp.data.MyModelX").data.id.replace(/-/g, ""),
        name: fileName,
        size: size,
        type: type,
        isCreate: true
    };
    testSystemAttachedStore.add(newRecord);
}
